﻿namespace _01.StringBuilderSubstring
{
    using System;
    using System.Text;

    public static class StringBuilderExtension
    {
        public static StringBuilder Substring(this StringBuilder inputString, int startIndex)
        {
            return Substring(inputString, startIndex, inputString.Length - startIndex);
        }

        public static StringBuilder Substring(this StringBuilder inputString, int startIndex, int length)
        {
            if (startIndex < 0 || startIndex > inputString.Length)
            {
                throw new ArgumentOutOfRangeException("startIndex", "Out of range");
            }
            else if (length < 0 || startIndex > inputString.Length - length)
            {
                throw new ArgumentOutOfRangeException("lenght", "Out of range");
            }

            if (inputString.Length == 0)
            {
                return new StringBuilder();
            }

            StringBuilder substring = new StringBuilder();
            int endIndex = startIndex + length;
            for (int i = startIndex; i < endIndex; i++)
            {
                substring.Append(inputString[i]);
            }

            return substring;
        }
    }
}